package com.shenfa.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.ibatis.sqlmap.client.SqlMapClient;

import com.shenfa.bean.Emprole;
/**
 * 部门角色人员关联表
 * @author Administrator
 *
 */
@SuppressWarnings("unchecked")
public class EmproleDao {
	@Autowired
	private SqlMapClient sqlMapClient;
	/**
	 * 查询部门角色人员关联表，查询条件为对象的所有属性，没有值的部做为查询条件
	 * @param emp
	 * @return
	 */
	public List<Emprole> getList(Emprole emp){
		List<Emprole> list=null;
		try {
			list= sqlMapClient.queryForList("emprole_manager.get",emp);
		} catch (SQLException e) {
			return list;
		}
		return list;
	}
	/**
	 * 根据部门ID获取角色信息
	 * @param id
	 * @return
	 */
	public List<Emprole> getEmproleList(String id, int start, int limit){
		List<Emprole> list=null;
		try {
			list= sqlMapClient.queryForList("emprole_manager.getEmproleList",id,start,limit);
		} catch (SQLException e) {
			return list;
		}
		return list;
	}
	public int getCount(String id){
		try {
			return (Integer)sqlMapClient.queryForObject("emprole_manager.getCount", id);
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
	}
	/**
	 * 根据是否存在相关数据
	 * @param emprole
	 */
	public String isExist(Emprole emprole){
	try {
			return sqlMapClient.queryForObject("emprole_manager.isexist",emprole).toString();
		} catch (SQLException e) {
			e.printStackTrace();
			return "";
		}
	}
	/**
	 * 根据人员ID和部门ID查询数据
	 * @param emprole
	 */
	public Emprole getEmproleByPidDid(String pid,String did){
		Emprole emprole=new Emprole();
		emprole.setPid(pid);
		emprole.setDid(did);
		try {
			return (Emprole)sqlMapClient.queryForObject("emprole_manager.getEmproleByPidDid",emprole);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	public List<Emprole> listRoleByDid(String did){
		try {
			return sqlMapClient.queryForList("emprole_manager.listRoleByDid",did);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 删除一个用户的角色关联信息
	 * @param uid
	 * @return
	 */
	public int delRoleByUid(String uid){
		try {
			return sqlMapClient.delete("emprole_manager.delRoleByUid",uid);
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
	}
	/**
	 * 删除所有角色关联信息
	 * @param did
	 * @return
	 */
	public int delRoleByRid(String rid){
		try {
			return sqlMapClient.delete("emprole_manager.delRoleByRid",rid);
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
	}
	
	/**
	 * 根据角色ID查询用户列表
	 * @param rid
	 * @return
	 */
	public List<String> selectRoleByRid(String rid) {
		List<String> list = new ArrayList<String>();
		try{
			list=sqlMapClient.queryForList("emprole_manager.queryRoleByRid",rid);
		}catch(Exception e){
			e.printStackTrace();
			return list;
		}
		return list;
	}
	
	/**
	 * 验证角色名是否存在
	 * @param role
	 * @return
	 */
	public String checkRoleName(Emprole role) {
		try{
			return (String) sqlMapClient.queryForObject("emprole_manager.checkRoleName",role);
		}catch(Exception e){
			e.printStackTrace();
			return "1";
		}
	}
	
	/**
	 * 验证角色是否绑定用户
	 * @param role
	 * @return
	 */
	public String checkDelRole(Emprole role) {
		try{
			return (String) sqlMapClient.queryForObject("emprole_manager.checkDelRole",role);
		}catch(Exception e){
			e.printStackTrace();
			return "1";
		}
	}
	/**
	 * 验证用户是否被赋予了角色
	 * @param role
	 * @return
	 */
	public String checkDelPerson(Emprole role) {
		try{
			return (String) sqlMapClient.queryForObject("emprole_manager.checkDelPerson",role);
		}catch(Exception e){
			e.printStackTrace();
			return "1";
		}
	}
	
}
